/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author cegarciaa
 */
public class DAO {

    public DAO() {
        emf = Persistence.createEntityManagerFactory("ServerComunicacion2PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Object obj) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Object obj) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            obj = em.merge(obj);
            em.getTransaction().commit();
        } catch (Exception ex) {
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Object obj;
            obj = em.getReference(Object.class, id);
            em.remove(obj);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public Object findObject(Long id, Object o) {
        EntityManager em = getEntityManager();
        try {
            return em.find(o.getClass(), id);
        } finally {
            em.close();
        }
    }

     public Object findObject(Integer id, Object o) {
        EntityManager em = getEntityManager();
        try {
            return em.find(o.getClass(), id);
        } finally {
            em.close();
        }
    }
     
    public Object findObjectByParejaPK(Object PK, Object o) {
        EntityManager em = getEntityManager();
        try {
            return em.find(o.getClass(), PK);
        } finally {
            em.close();
        }
    }

    public Object findAlarmaByIdDispositivoIdVariable(Long iddispositivo, Long idvariablef){
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select o from Alarmavariable o where o.iddispositivo.iddispositivo =:iddisp and o.idvarf.idvarf=:idvar");
            q.setParameter("iddisp", iddispositivo);
            q.setParameter("idvar", idvariablef);
            return q.getSingleResult();
        }catch(NoResultException nr){
            return null;
        }finally {
            em.close();
        }
    }
    
    public int numeroVariablesEquipo(int idDispositivo){
        EntityManager em = getEntityManager();
        try{
            Query q = em.createQuery("select count(o) from Señales o where o.señalesPK.equiposid =:iddisp");
            q.setParameter("iddisp", idDispositivo);
            return Integer.parseInt(q.getSingleResult().toString());
        }catch(NoResultException nr){
            return 0;
        }finally {
            em.close();
        }
    }

    public void cerrarConexionBD(){
        emf.close();//no hace absolutamente nada
    }
}
